package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Ctchemical;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class CoalChemicalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<Ctchemical> ctchemicalList;
    private Context mContext;

    public CoalChemicalAdapter(List<Ctchemical> list, Context context) {
        this.ctchemicalList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (ctchemicalList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.coal_chemical_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(ctchemicalList.get(position));
            viewHolder.SetViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (ctchemicalList.size() > 0) {
            return ctchemicalList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        return 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView arrowIv;
        TextView timeTv;
        TextView companyTv;
        ExpandView expandView;
        TextView statusLabelTv;
        TextView effectTimeTV;
        TextView timeTV;
        TextView coalItemNameTV;
        TextView flatTypeTV;
        TextView flatBaseTV;
        TextView downValueTV;
        TextView upValueTV;
        TextView rapTV;
        TextView finedTypeTV;
        TextView finedValueTV;
        TextView unitTV;
        TextView resultTypeTV;
        TextView decimalDigitTV;
        TextView wetanddryTV;


        public ViewHolder(View itemView) {
            super(itemView);
            arrowIv = (ImageView) itemView.findViewById(R.id.arrow);
            timeTv = (TextView) itemView.findViewById(R.id.add_time);
            companyTv = (TextView) itemView.findViewById(R.id.company);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            statusLabelTv = (TextView) itemView.findViewById(R.id.status_label);
            effectTimeTV = (TextView) itemView.findViewById(R.id.effect_time);
            timeTV = (TextView) itemView.findViewById(R.id.add_time);
            coalItemNameTV = (TextView) itemView.findViewById(R.id.coal_item_name);
            flatTypeTV = (TextView) itemView.findViewById(R.id.flat_type);
            flatBaseTV = (TextView) itemView.findViewById(R.id.flat_base);
            downValueTV = (TextView) itemView.findViewById(R.id.down_value);
            upValueTV = (TextView) itemView.findViewById(R.id.up_value);
            rapTV = (TextView) itemView.findViewById(R.id.rap);
            finedTypeTV = (TextView) itemView.findViewById(R.id.fined_type);
            finedValueTV = (TextView) itemView.findViewById(R.id.fined_value);
            unitTV = (TextView) itemView.findViewById(R.id.unit);
            resultTypeTV = (TextView) itemView.findViewById(R.id.resoult_type);
            decimalDigitTV = (TextView) itemView.findViewById(R.id.decimal_digit);
            wetanddryTV = (TextView) itemView.findViewById(R.id.wetanddry);
        }

        public void BindViewData(Ctchemical ctchemical) {
            effectTimeTV.setText("生效时间：" + TimeUtils.parseDateTime(ctchemical.getEffecttime()));
            coalItemNameTV.setText("煤种:" + ctchemical.getCoalname() + " "
                    + "化验项:" + ctchemical.getItemname());
            timeTv.setText(TimeUtils.parseDateTime(ctchemical.getAddtime()) + "");
            //0、不浮动，1、低上限值浮动；3、超下限值浮动
            String flattype = "";
            switch (ctchemical.getFlattype()) {
                case 0:
                    flattype = "不浮动";
                    break;
                case 1:
                    flattype = "低上限值浮动";
                    break;
                case 2:
                    flattype = "超下限值浮动";
                    break;
            }
            flatBaseTV.setText("浮动方试:" + flattype);
            flatBaseTV.setText("浮动基数:" + ctchemical.getFlatbase() + "");
            downValueTV.setText("下限值:" + ctchemical.getDown() + "");
            upValueTV.setText("上限值:" + ctchemical.getUp() + "");
            String rap = "";
            //0、不奖不罚，1、奖励，2、罚款
            switch (ctchemical.getRap()) {
                case 0:
                    rap = "不奖不罚";
                    break;
                case 1:
                    rap = "奖励";
                    break;
                case 2:
                    rap = "罚款";
                    break;
            }
            rapTV.setText("奖罚方试:" + rap);
            //1、单价；2、吨数
            if (ctchemical.getFinedtype() == 1) {
                flatTypeTV.setText("奖罚类型:单价");
            } else {
                flatTypeTV.setText("奖罚类型:吨数");
            }
            finedValueTV.setText("扣罚值:" + ctchemical.getFinedvalue());
            unitTV.setText("单位：" + ctchemical.getUnit());

            String resoultType = "";
            //0、正常取值，1、四舍五入，2、截断，3、进位
            switch (ctchemical.getResoulttype()) {
                case 0:
                    resoultType = "正常取值";
                    break;
                case 1:
                    resoultType = "四舍五入";
                    break;
                case 2:
                    resoultType = "截断";
                    break;
                case 3:
                    resoultType = "进位";
                    break;
            }
            resultTypeTV.setText("化验取值：" + resoultType);
            decimalDigitTV.setText("小数位数：" + ctchemical.getDecimaldigit());
            wetanddryTV.setText("干湿基:" + (ctchemical.getWetanddry() == 1 ? "干基" : "湿基"));
            statusLabelTv.setText("");
        }

        public void SetViewListener(final int position) {
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.arrow:
                            if (expandView.isExpand()) {
                                expandView.collapse();
                                arrowIv.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                            } else {
                                expandView.expand();
                                arrowIv.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                            }
                            break;
                    }
                }
            };
            arrowIv.setOnClickListener(listener);
        }
    }
}
